package com.music.note_android.data.database.dao

import androidx.room.*
import com.music.note_android.data.model.Note
import kotlinx.coroutines.flow.Flow

/**
 * 笔记数据访问对象
 * 定义对笔记表的所有数据库操作
 */
@Dao
interface NoteDao {
    
    /**
     * 获取所有笔记，按修改时间倒序排列
     */
    @Query("SELECT * FROM notes ORDER BY modified_time DESC")
    fun getAllNotes(): Flow<List<Note>>
    
    /**
     * 根据ID获取单个笔记
     */
    @Query("SELECT * FROM notes WHERE id = :noteId")
    suspend fun getNoteById(noteId: String): Note?
    
    /**
     * 搜索笔记（根据标题模糊匹配）
     */
    @Query("SELECT * FROM notes WHERE title LIKE '%' || :query || '%' ORDER BY modified_time DESC")
    suspend fun searchNotes(query: String): List<Note>
    
    /**
     * 根据文件夹ID获取笔记
     */
    @Query("SELECT * FROM notes WHERE folder_id = :folderId ORDER BY modified_time DESC")
    suspend fun getNotesByFolder(folderId: String): List<Note>
    
    /**
     * 根据文件夹ID获取笔记的Flow
     */
    @Query("SELECT * FROM notes WHERE folder_id = :folderId ORDER BY modified_time DESC")
    fun getNotesByFolderFlow(folderId: String): Flow<List<Note>>
    
    /**
     * 获取根目录下的笔记（无文件夹）
     */
    @Query("SELECT * FROM notes WHERE folder_id IS NULL ORDER BY modified_time DESC")
    suspend fun getRootNotes(): List<Note>
    
    /**
     * 插入新笔记
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertNote(note: Note)
    
    /**
     * 批量插入笔记
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertNotes(notes: List<Note>)
    
    /**
     * 更新笔记
     */
    @Update
    suspend fun updateNote(note: Note)
    
    /**
     * 删除笔记
     */
    @Delete
    suspend fun deleteNote(note: Note)
    
    /**
     * 根据ID删除笔记
     */
    @Query("DELETE FROM notes WHERE id = :noteId")
    suspend fun deleteNoteById(noteId: String)
    
    /**
     * 批量删除笔记
     */
    @Query("DELETE FROM notes WHERE id IN (:noteIds)")
    suspend fun deleteNotesByIds(noteIds: List<String>)
    
    /**
     * 更新笔记标题
     */
    @Query("UPDATE notes SET title = :title, modified_time = :modifiedTime WHERE id = :noteId")
    suspend fun updateNoteTitle(noteId: String, title: String, modifiedTime: Long)
    
    /**
     * 更新笔记缩略图路径
     */
    @Query("UPDATE notes SET thumbnail_path = :thumbnailPath, modified_time = :modifiedTime WHERE id = :noteId")
    suspend fun updateNoteThumbnail(noteId: String, thumbnailPath: String, modifiedTime: Long)
    
    /**
     * 移动笔记到指定文件夹
     */
    @Query("UPDATE notes SET folder_id = :folderId, modified_time = :modifiedTime WHERE id = :noteId")
    suspend fun moveNoteToFolder(noteId: String, folderId: String?, modifiedTime: Long)
    
    /**
     * 批量移动笔记到指定文件夹
     */
    @Query("UPDATE notes SET folder_id = :folderId, modified_time = :modifiedTime WHERE id IN (:noteIds)")
    suspend fun moveNotesToFolder(noteIds: List<String>, folderId: String?, modifiedTime: Long)
    
    /**
     * 获取笔记总数
     */
    @Query("SELECT COUNT(*) FROM notes")
    suspend fun getNotesCount(): Int
    
    /**
     * 获取指定文件夹下的笔记数量
     */
    @Query("SELECT COUNT(*) FROM notes WHERE folder_id = :folderId")
    suspend fun getNotesCountInFolder(folderId: String): Int
    
    /**
     * 清空所有笔记
     */
    @Query("DELETE FROM notes")
    suspend fun deleteAllNotes()
}